package servlet;

import dao.PowerBatteryMapper;
import entity.PowerBattery;
import utils.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = "/UpdateServlet")
public class UpdateServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        sqlSession.getMapper(PowerBatteryMapper.class).powerUpdate(new PowerBattery(
                Integer.parseInt(req.getParameter("id")),
                req.getParameter("carName"),
                req.getParameter("cityName"),
                req.getParameter("userName"),
                Integer.parseInt(req.getParameter("pressureValue")),
                req.getParameter("time")
        ));
        sqlSession.commit();
        MyBatisUtil.closeSqlSession(sqlSession);
        resp.sendRedirect("ListServlet");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
